﻿using LiteBlog.Common.Entities;
using LiteBlog.Components.Contracts;
using LiteBlog.Controllers.Contracts;
using LiteBlog.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteBlog.Controllers.Tests
{
    [TestClass]
    public class ArchiveControllerTests : ControllerTests<IArchiveController>
    {
        static readonly int Year = 2012;
        static readonly int Month = 12;

        [TestMethod]
        public void ArchiveGetsPublished()
        {
            _controller.Archive();
            var mock = Mock.Get<IBlogComp>(_controller.BlogComponent);
            mock.Verify(b => b.GetPublished());
        }

        [TestMethod]
        public void IndexGetsPostsByMonth()
        {
            _controller.Index(Year, Month, null);
            var mock = Mock.Get<IBlogComp>(_controller.BlogComponent);
            mock.Verify(b => b.GetPostsByMonth(Year, Month));
        }

        [TestMethod]
        public void IndexGetsSettings()
        {
            _controller.Index(Year, Month, null);
            var mock = Mock.Get<ISettingsComp>(_controller.SettingsComponent);
            mock.Verify(se => se.Load());
        }

        [TestMethod]
        public void IndexDoesNotGetAuthorIfUnauthenticated()
        {
            _controller.Index(Year, Month, null);
            var mock = Mock.Get<IAuthorComp>(_controller.AuthorComponent);
            mock.Verify(au => au.Get(It.IsAny<string>()), Times.Never);
        }

        [TestMethod]
        public void IndexGetsAuthorIfAuthenticated()
        {
            _controller.UserModel = new UserModel("Admin", true);
            _controller.Index(Year, Month, null);
            var mock = Mock.Get<IAuthorComp>(_controller.AuthorComponent);
            mock.Verify(au => au.Get("Admin"), Times.Once);
        }

        [TestMethod]
        public void IndexGetsPost()
        {
            var postInfos = new List<PostInfo>();
            postInfos.Add(new PostInfo { FileID = "Sample-Post" });
            var blogMock = Mock.Get<IBlogComp>(_controller.BlogComponent);
            blogMock.Setup(b => b.GetPostsByMonth(It.IsAny<int>(), It.IsAny<int>()))
                    .Returns(postInfos);
            var postMock = Mock.Get<IPostComp>(_controller.PostComponent);
            postMock.Setup(p => p.Load(It.IsAny<string>()))
                    .Returns(new Post());
            _controller.Index(Year, Month, null);
            postMock.Verify(p => p.Load("Sample-Post"));
        }

        [TestMethod]
        public void GetMoreGetsPostsByMonth()
        {
            _controller.GetMore(Year, Month, 1);
            var mock = Mock.Get<IBlogComp>(_controller.BlogComponent);
            mock.Verify(b => b.GetPostsByMonth(Year, Month));
        }

        [TestMethod]
        public void GetMoreGetsPost()
        {
            _settings.PostCount = 1;
            var postInfos = new List<PostInfo>();
            postInfos.Add(new PostInfo());
            postInfos.Add(new PostInfo { FileID = "Sample-Post" });
            var blogMock = Mock.Get<IBlogComp>(_controller.BlogComponent);
            blogMock.Setup(b => b.GetPostsByMonth(It.IsAny<int>(), It.IsAny<int>()))
                    .Returns(postInfos);
            var postMock = Mock.Get<IPostComp>(_controller.PostComponent);
            postMock.Setup(p => p.Load(It.IsAny<string>()))
                    .Returns(new Post());
            _controller.GetMore(Year, Month, 1);
            postMock.Verify(p => p.Load("Sample-Post"));
        }

        [TestMethod]
        public void GetMoreGetsSettings()
        {
            _controller.GetMore(Year, Month, 0);
            var mock = Mock.Get<ISettingsComp>(_controller.SettingsComponent);
            mock.Verify(se => se.Load());
        }
    }
}
